﻿/****---------------------------------------------------------------------------------------------------->>>>
CutString for Flash 8 v1.0
Coded to work with ActionScript 2.0
-------------------------------------------------- 
	Created by 
		Lance Den (vistaden@msn.com , yanjun@vika.cn)
		SIAN Digital Technology co.,ltd. (www.vika.cn)

	Based on code by
		maybe is kcly (www.luar.com.hk)

	Version History:
		2007-06-14 - Modify some features.
		2007-04-07 - Modify some features,and make the usage of the class
					 to an easy way.
		2006-10-11 - First get this file from kcly

	Authors notes: 
		Class Initialization:	
			- Create an instance of the CutString class
			
			Example:
				import Classes.Utils.CutString;
				import mx.utils.TextDot;
				import mx.utils.TextDot;
				import mx.utils.TextDot;
				var CutString:CutString = new CutString(this);
			Note:
				CutString(tTarget:MovieClip,tWidth:Number,tText:String, 
				tColor:Number, txtColor:Number)
				the first and the second parameter(tTarget=this,or _root)
				can't be undefined.
			
		Draw a tip on Stage:
			
			Example:
				
	
			 
		Remove a tip on Stage:
			
			Example:
				

		- Lance Den 2007-04-06
<<<<----------------------------------------------------------------------------------------------------****/

import mx.utils.TextDot;

class mx.utils.CutString
{
	/*------------------------------------------------------------------------>
	* PROPERTIES
	<------------------------------------------------------------------------*/
	var num_length:Number;
	
	/*------------------------------------------------------------------------>
	* Class Constructor
	<------------------------------------------------------------------------*/
	function CutString()
	{
		this.length = 5;
	}
	
	/*------------------------------------------------------------------------>
	* public
	<------------------------------------------------------------------------*/
	public function runCut(str_t:String, num_t:Number):String
	{
		var num_l:Number = (num_t==undefined) ? this.length*2 : num_t;
		if (str_t == undefined) str_t = "";
		var num:Number = 0;
		for (var i = 0; i < str_t.length; i++) {
			if (str_t.charCodeAt(i) > 256) {
				num += 2;
			}else {
				num++;
			}
			if (num > num_l) {
				if (str_t.charCodeAt(i) > 256) {
					num -= 2;
				}else {
					num--;
				}
				if (str_t.charCodeAt(i - 1) > 256) {
					num -= 2;
				}else {
					num--;
				}
				if (num > num_l - 2) {
					return str_t.substr(0, i - 2) + "..";
				}else {
					return str_t.substr(0, i - 1) + "..";
				}
			}
		}
		return str_t;
	}
	
	/*------------------------------------------------------------------------>
	* setter and getter
	<------------------------------------------------------------------------*/
	public function set length(num_t:Number):Void
	{
		this.num_length = num_t;
	}
	
	public function get length():Number
	{
		return this.num_length;
	}
	
	/*------------------------------------------------------------------------>
	* private
	<------------------------------------------------------------------------*/
	
}
